/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddToDynamicDomainForm.java
 *
 * Created on 2009-2-10, 21:10:32
 */

package com.guzzservices.easydomain.ui.frame;

import com.guzzservices.easydomain.logic.DomainManager;
import com.guzzservices.easydomain.logic.domain.DynamicDomain;
import com.guzzservices.easydomain.logic.domain.RecordA;
import com.guzzservices.easydomain.util.ComponentCenter;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */
public class AddToDynamicDomainForm extends javax.swing.JDialog {

    /** Creates new form AddToDynamicDomainForm
     * @deprecated for netbeans to design
     */
    public AddToDynamicDomainForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public AddToDynamicDomainForm(java.awt.Frame parent, RecordA record, boolean modal) {
        super(parent, modal);
        this.record = record ;
        initComponents();
        initData() ;
    }

    protected void initData(){
        this.setLocation(this.getOwner().getX() + 20, this.getOwner().getY() + 35);
        
        this.swingFullDomainLabel.setText(this.record.getVisitedDomain()) ;

        this.activeStatusSwingCombo.setSelectedIndex(1) ;
        this.activeStatusSwingCombo.setEnabled(true);
        this.swingProductStatusLabel.setText("请选择是否立即激活选择的动态域名，自动同步IP") ;

        try {
            Enumeration<NetworkInterface> el = NetworkInterface.getNetworkInterfaces();

            List eths = new LinkedList() ;

            String defaultDevice = null ;

             while (el.hasMoreElements()) {
                 NetworkInterface net = el.nextElement() ;

                 String name = net.getDisplayName() ;

                 if(name == null){
                    name = net.getName() ;
                 }else{
                    name = name + "--" + net.getName() ;
                 }

                 Enumeration<InetAddress> ips = net.getInetAddresses() ;
                 String IP = "" ;
                 while(ips.hasMoreElements()){
                    IP = ips.nextElement().getHostAddress() ;
                 }

                 String showName = name + "(" + IP + ")" ;
                 eths.add(showName) ;

                 ethData.add(net.getName()) ;

                 if("eth0".equals(net.getName())){
                    defaultDevice = showName ;
                 }

             }

             eths.add("局域网对外IP--lan(" + DomainManager.getDomainManager().getLanIP() + ")") ;
             ethData.add("lan") ;
            
            ethSwingList.setModel(new javax.swing.DefaultComboBoxModel(eths.toArray()));

            if(defaultDevice != null){
                this.ethSwingList.setSelectedItem(defaultDevice) ;
            }
            
        } catch (SocketException ex) {
            Logger.getLogger(AddToDynamicDomainForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        swingFullDomainLabel = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        ethSwingList = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        swingProductStatusLabel = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        activeStatusSwingCombo = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(com.guzzservices.easydomain.ui.frame.DDSProjectApp.class).getContext().getResourceMap(AddToDynamicDomainForm.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        swingFullDomainLabel.setText(resourceMap.getString("swingFullDomainLabel.text")); // NOI18N
        swingFullDomainLabel.setName("swingFullDomainLabel"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        ethSwingList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        ethSwingList.setName("ethSwingList"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        swingProductStatusLabel.setText(resourceMap.getString("swingProductStatusLabel.text")); // NOI18N
        swingProductStatusLabel.setName("swingProductStatusLabel"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        activeStatusSwingCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "暂停使用", "激活" }));
        activeStatusSwingCombo.setEnabled(false);
        activeStatusSwingCombo.setName("activeStatusSwingCombo"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel1)
                    .addComponent(jButton1))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(swingFullDomainLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE))
                            .addComponent(ethSwingList, 0, 469, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(activeStatusSwingCombo, 0, 100, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(swingProductStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 365, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jButton2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(swingFullDomainLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ethSwingList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(swingProductStatusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(activeStatusSwingCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int selectDeviceIndex = this.ethSwingList.getSelectedIndex() ;
        if(selectDeviceIndex < 0){
            return ;
        }

        if(ethData != null && ethData.size()>selectDeviceIndex){
            String selectedDevice = ethData.get(selectDeviceIndex) ;
            DynamicDomain dd = new DynamicDomain(this.record, selectedDevice) ;

            dd.setActive(this.activeStatusSwingCombo.getSelectedIndex()) ;

            DomainManager.getDomainManager().addDynamicDomain(dd) ;

            ComponentCenter.getInstance().setTextToStatusPanel(dd.getVisitedDomain() + "升级为动态域名，并绑定到网卡端口：" + dd.getNetworkDevice()) ;
        }

        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                AddToDynamicDomainForm dialog = new AddToDynamicDomainForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox activeStatusSwingCombo;
    private javax.swing.JComboBox ethSwingList;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel swingFullDomainLabel;
    private javax.swing.JLabel swingProductStatusLabel;
    // End of variables declaration//GEN-END:variables

    private RecordA record ;
    private List<String> ethData = new LinkedList<String>() ;

}
